﻿using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class SmsInfoBLL
    {
        #region 构造单例
        private SmsInfoBLL() { }
        private static SmsInfoBLL _instance;
        public static SmsInfoBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new SmsInfoBLL());
            }
        }
        #endregion


        public int Add(List<Smsinfo> infos)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                foreach (var item in infos)
                {
                    if (item.TargetUserId.HasValue && item.TargetUserId > 0)
                    {
                        item.TargetDepId = ef.Userinfo.Where(p => p.UserId == item.TargetUserId).FirstOrDefault()?.DepId;
                    }

                    if (item.SendUserId.HasValue && item.SendUserId > 0)
                    {
                        item.SendDepId = (ef.Userinfo.Where(p => p.UserId == item.SendUserId.Value).FirstOrDefault()?.DepId).Value;
                    }

                    ef.Smsinfo.Add(item);
                    ef.Entry<Smsinfo>(item).State = EntityState.Added;
                    ef.SaveChanges();
                }
                return 1;
            }
        }

        public int Remove(int smsId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Smsinfo.Where(p => p.SmsId == smsId).FirstOrDefault();
                if (exist != null)
                {
                    ef.Smsinfo.Add(exist);
                    ef.Entry<Smsinfo>(exist).State = EntityState.Deleted;
                    return ef.SaveChanges();
                }
                else
                {
                    return 0;
                }
            }
        }

        public ResultDto<dynamic, dynamic> GetByPage(int sendUserId,int targetUserId,int sendDepId,int targetDepId, int searchType,string addressCounty ,RequestPageBase page) 
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            var sql = "  select a.*,c.RealName as SendUserName,d.DepName as TargetDepName,f.DepName as SendDepName,b.AddressCounty as SendAddressCounty,g.LinchpinName as SendAddressCountyName from smsinfo as a   ";
            sql += " left join userinfo as b on a.TargetUserID =b.UserID  ";
            sql += " left join userinfo as c on a.SendUserID =c.UserID ";
            sql += " left join department as d on b.DepID=d.DepID  ";
            sql += " left join department as f on c.DepID=f.DepID  ";
            sql += " left join linchpin as g on b.AddressCounty=g.LinchpinID ";
            sql += " where 1=1 ";
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                string paramsSql = string.Empty;
                if (!string.IsNullOrEmpty(page.Keyword))
                {
                    paramsSql += $" a.MobileCode like '%{page.Keyword}%'";
                    paramsSql += $" or a.TargetUserName like '%{page.Keyword}%'";
                    paramsSql += $" or c.RealName like '%{page.Keyword}%'";
                    paramsSql += $" or d.DepName like '%{page.Keyword}%'";
                    paramsSql += $" or f.DepName like '%{page.Keyword}%'";
                    paramsSql = $" and ({paramsSql})";
                }

                if (sendUserId > 0)
                {
                    paramsSql += $" and a.SendUserID='{sendUserId}'";
                }

                if (targetUserId > 0)
                {
                    paramsSql += $" and a.TargetUserID='{targetUserId}'";
                }

                if (sendDepId >0)
                {
                    paramsSql += $" and a.SendDepID='{sendDepId}'";
                }

                if (searchType == 1)
                {
                    paramsSql += $" and (a.TargetUserID !='' and a.TargetUserID  !='0' and a.TargetUserID is not null)";
                }
                else if (searchType==2)
                {
                    paramsSql += $" and (a.TargetUserID='' or a.TargetUserID ='0' or a.TargetUserID is null)";
                }

                if (targetDepId > 0 && searchType!=2)
                {
                    paramsSql += $" and a.TargetDepId='{targetDepId}'";
                }

                if (targetUserId > 0 && searchType != 2)
                {
                    paramsSql += $" and a.TargetUserID='{targetUserId}'";
                }

                if (!string.IsNullOrEmpty(addressCounty))
                {
                    paramsSql += $" and b.AddressCounty='{addressCounty}'";
                }

                var timeSql = page.TimeSql(" a.SendTime ");
                paramsSql += page.OrderByPattern == 0 ? "a.SendTime".OrderByDescending() : "a.SendTime".OrderBy();
                paramsSql += page.Limit();
                sql += timeSql;
                sql += paramsSql;
                var countSql = sql.CountSql();
                var list = ef.Database.SqlQuery<SmsInfoModel>(sql);
                result.FirstParam = list;
                result.SecondParam = ef.Database.SqlQuery(countSql).Rows[0][0];
            }
            return result;
        }

    }
}
